// pages/GenerateOrder/GenerateOrder.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Goods:[],
    input1:'',
    input2:'',
    input3:'',
    input4:'',
    color:'白色',
    size:'M',
    number:1,
    rmb:0,
    rmb2:0
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  input1:function(e){
    this.setData({
      input1: e.detail.value
    })
    console.log(e.detail.value)
  },
  input2:function(e){
    this.setData({
      input2: e.detail.value
    })
    console.log(e.detail.value)
  },
  input3:function(e){
    this.setData({
      input3: e.detail.value
    })
    console.log(e.detail.value)
  },
  input4:function(e){
    this.setData({
      input4: e.detail.value
    })
    console.log(e.detail.value)
  },
  jian:function(){
    if(this.data.number<=1){
      this.setData({
        number:1
      })
    }else{
      this.setData({
        number:this.data.number-1
      })
    }
  },
  jia:function(){
    this.setData({
      number:this.data.number+1
    })
  },
  fk:function(){
    var that=this
    wx.showModal({
      title: '付款',
      content: '是否进行支付',
      success: function (res) {
        wx.showLoading({
          title: '生成中',
        })
        if (res.confirm) {
          db.collection('Order').add({
            // data 字段表示需新增的 JSON 数据
            data: {
              sale:that.data.Goods[0].sale,
              color:that.data.color,
              number:that.data.number,
              size:that.data.size,
              CreationTime:that.getNowDatTime(),
              PaymentTime:that.getNowDatTime(),
              DeliveryTime:'',
              ReceivingTime:'',
              rmb:parseFloat(that.data.rmb),
              Consignee:that.data.input1,
              address:that.data.input2,
              Telephone:that.data.input3,
              Remarks:that.data.input4,
              GoodsId:that.data.Goods[0].id,
              SellOpenid:that.data.Goods[0].openid,
              state:'待发货',
              GoodsTitle:that.data.Goods[0].title,
              SellNickName:that.data.Goods[0].nickName,
              SellAvatarUrl:that.data.Goods[0].avatarUrl,
              GoodsImg:that.data.Goods[0].img[0],
              express:that.data.Goods[0].express,
            }
          })
          .then(res => {
            console.log(res)
            wx.hideLoading();
              wx.showToast({
                title: "付款成功",
              })
            setTimeout(function () {
              that.return()
            }, 1000)
          })
        } else {
          console.log(res.confirm)
          db.collection('Order').add({
            // data 字段表示需新增的 JSON 数据
            data: {
              sale:that.data.Goods[0].sale,
              color:that.data.color,
              number:that.data.number,
              size:that.data.size,
              CreationTime:that.getNowDatTime(),
              PaymentTime:'',
              DeliveryTime:'',
              ReceivingTime:'',
              rmb:parseFloat(that.data.rmb),
              Consignee:that.data.input1,
              address:that.data.input2,
              Telephone:that.data.input3,
              Remarks:that.data.input4,
              GoodsId:that.data.Goods[0].id,
              SellOpenid:that.data.Goods[0].openid,
              state:'待付款',
              GoodsTitle:that.data.Goods[0].title,
              SellNickName:that.data.Goods[0].nickName,
              SellAvatarUrl:that.data.Goods[0].avatarUrl,
              GoodsImg:that.data.Goods[0].img[0],
              express:that.data.Goods[0].express,
            }
          })
          .then(res => {
            console.log(res)
            wx.hideLoading();
              wx.showToast({
                title: "订单生成",
              })
            setTimeout(function () {
              that.return()
            }, 1000)
          })
        }
      }
    })
  },
  //获取当前时间
  getNowDatTime:function(){
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate<=9) {
      strDate = "0" + strDate;
    }
    var time = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
    console.log(time);
    return time;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.color)
    console.log(options.size)
    console.log(options.number)
    console.log(options.rmb)
    console.log(options.id)
    console.log(options.sale)
    this.setData({
      number:options.number,
      color:options.color,
      size:options.size
    })
    this.getGoods(options.id)
  },
getGoods: function (id) {
    console.log(id)
    db.collection('Goods').doc(id)
      .get().then(res => {
        console.log(res.data.fileIDs)
        this.setData({
          'order[0].color':res.data.color[0],
          'order[0].size':res.data.size[0],
          'Goods[0].img': res.data.fileIDs,
          'Goods[0].id': res.data._id,
          'Goods[0].img2': res.data.fileIDs2,
          'Goods[0].title': res.data.title,
          'Goods[0].color': res.data.color,
          'Goods[0].rmb': res.data.rmb,
          'Goods[0].openid': res.data._openid,
          rmb:(res.data.rmb/10.0*res.data.sale).toFixed(2),
          rmb2:(res.data.rmb/10.0*res.data.sale+res.data.express).toFixed(2),
          'Goods[0].sale': res.data.sale,
          'Goods[0].sell': res.data.sell,
          'Goods[0].liulan': res.data.liulan,
          'Goods[0].size':res.data.size,
          'Goods[0].address':res.data.address,
          'Goods[0].express':res.data.express,
          'Goods[0].nickName':res.data.nickName,
          'Goods[0].avatarUrl':res.data.avatarUrl,
        })
        console.log(this.data.Goods)
      })
      .catch(err => {
        console.error(err)
        wx.hideLoading();
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})